﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web;
using Newtonsoft.Json;
namespace CNKI.TPI.Web.Base.HtmlHelp
{
    public static class TextHelper
    {
        /// <summary>
        /// 三目运算符
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string TernaryOperator(this HtmlHelper helper, object str, string _value)
        {
            if (str != null && str.ToString() == _value)
            {
                return "<span style='color: red'>是</span>";
            }
            else
            {
                return "<span>否</span>";
            }
        }
        /// <summary>
        /// 日期格式
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string DateFormate(this HtmlHelper helper, DateTime? date, string formartP = "yyyy-MM-dd")
        {
            if (date == null)
            {
                return "";
            }

            return string.Format("{0:" + formartP + "}", date);
        }
        /// <summary>
        /// 日期格式
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string DateTimeFormate(this HtmlHelper helper, DateTime? date)
        {
            if (date == null)
            {
                return "";
            }
            return string.Format("{0:yyyy-MM-dd HH:mm:ss}", date);
        }

        /// <summary>
        /// 截断字符
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string SubString(this HtmlHelper helper, object str, int count, bool isTrue = true)
        {
            if (str != null && str.ToString() != "")
            {
                if (isTrue)
                {
                    str = StringHelper.RemoveHTML(str.ToString());
                }

                if (str.ToString().Length > count)
                {
                    return str.ToString().Remove(count) + "...";
                }
                else
                {
                    return str.ToString();
                }
            }
            else
            {
                return "";
            }

        }
        public static string ImageText(this HtmlHelper helper, object path, int width, int heigth)
        {
            if (path == null || path.ToString() == "")
            {
                return "";
            }
            else
            {
                return string.Format("<img src='{0}' style='width:{1}px; height:{2}px; '/>", path.ToString(), width, heigth);
            }
        }
        /// <summary>
        /// 枚举类型显示描述名称
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <param name="helper"></param>
        /// <param name="_value"></param>
        /// <returns></returns>
        public static string EnumDescriptionText<TEnum>(this HtmlHelper helper, int? _value)
        {
            IEnumerable<TEnum> values = Enum.GetValues(typeof(TEnum))
              .Cast<TEnum>();
            var query = values.FirstOrDefault(e => Convert.ToInt32(e) == _value);
            if (query == null)
                return "";
            else
            {
                return EnumDescription.GetFieldText(query);
            }
        }
        /// <summary>
        /// 枚举类型显示描述名称
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <param name="helper"></param>
        /// <param name="_value"></param>
        /// <returns></returns>
        public static string EnumDescriptionText<TEnum>(this HtmlHelper helper, string _value)
        {
            IEnumerable<TEnum> values = Enum.GetValues(typeof(TEnum))
              .Cast<TEnum>();
            var query = values.FirstOrDefault(e => Convert.ToInt32(e) == int.Parse(_value));
            if (query == null)
                return "";
            else
            {
                return EnumDescription.GetFieldText(query);
            }
        }

        /// <summary>
        /// 页面跳转
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        /// 
        public static string Redirect(this HtmlHelper helper, string url)
        {
            string str = string.Format("<script type='text/javascript'>window.location.href ='{0}'</script> ", url);
            return str;
        }
        /// <summary>
        /// 页面跳转
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static void Redirect(this HtmlHelper helper, string url, object param)
        {
            string rRrl = url + "?";
            rRrl += param.ToString().Replace("{", "").Replace("}", "").Replace(',', '&').Replace(" ", "").Trim();
            HttpContext.Current.Response.Redirect(rRrl);
        }
    }
}
